<?php
namespace app\platform\controller;

use think\cache\driver\Redis;
class Fun
{
    //检测跨域
    public static function origin(){
        $origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
        $allow_origin = array(
            'http://newmerchant.speeda.cn',
            'http://newbusiness.speeda.cn',
            'http://114.55.60.173',
            'http://a.roy.com'
        );
        if(in_array($origin, $allow_origin)){
            header('Access-Control-Allow-Origin:'.$origin);
        }else{
            die('Invalid Access');
        }
    }

    //科学计算转换
    static function sctonum($num, $double = 10){
        if(false !== stripos($num, "e")){
            $a = explode("e",strtolower($num));
            return bcmul($a[0], bcpow(10, $a[1], $double), $double);
        }
    }

    // p 目标点坐标   poly  数组(围栏点) lat 经度  lng 纬度  intersectCount bool变量
    //判断是否在围栏内
    public static function isInsidePolygons($p,$poly){
        $N = count($poly);
        $precision = 2e-10;
        $p1 = $poly[0];
        $p2 = 0;
        $intersectCount = 0;

        for($i=1;$i<=$N;$i++){
            if($p==$p1){
                return true;
            }
            $ii = $i % $N;
            $p2 = $poly[$ii];
            if($p['lat'] < min($p1['lat'],$p2['lat']) || $p['lat'] > max($p1['lat'],$p2['lat'])){
                $p1 = $p2;
                continue;
            }
            if($p['lat'] > min($p1['lat'],$p2['lat']) && $p['lat'] < max($p1['lat'],$p2['lat'])){
                if($p['lng'] <= max($p1['lng'],$p2['lng'])){
                    if($p1['lng'] == $p2['lng']){
                        if($p1['lng'] == $p['lng']){
                            return true;
                        }else{
                            ++$intersectCount;
                        }
                    }else{
                        $xinters = ($p['lat'] - $p1['lat']) * ($p2['lng'] - $p1['lng']) / ($p2['lat'] - $p1['lat']) + $p1['lng'];
                        if(abs($p['lng']-$xinters)<$precision){
                            return true;
                        }
                        if($p['lng'] < $xinters){
                            ++$intersectCount;
                        }
                    }
                }
            }else{
                if($p1['lat'] == $p2['lat'] && $p['lng'] >= min($p1['lng'],$p2['lng'])){
                    return true;
                }
                if($p['lat'] == $p2['lat'] && $p['lng'] <= $p2['lng']){
                    $ii2 = ($i+1) % $N;
                    $p3 = $poly[$ii2];
                    if($p['lat'] >= min($p1['lat'],$p3['lat']) && $p['lat'] <= max($p1['lat'],$p3['lat'])){
                        ++$intersectCount;
                    }else{
                        $intersectCount += 2;
                    }
                }
            }
            $p1 = $p2;
        }

        $ppp = $intersectCount % 2;
        if($ppp==0){
            return false;
        }else{
            return true;
        }
    }

}